package be.fomp.jeve.application.data.standard.characterids;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;

import be.fomp.jeve.application.data.AbstractResultImpl;
import be.fomp.jeve.application.data.standard.CharacterIDs;
import be.fomp.jeve.core.exceptions.JEveException;

public final class CharacterIDsImpl extends AbstractResultImpl implements CharacterIDs {

private List<Character> characters = new ArrayList<Character>();
	static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public CharacterIDsImpl(Document xmlDocument) throws JEveException{
		super(xmlDocument);
		
		Element root = xmlDocument.getRootElement();
		
		List<Element> resultSet = root.getChild("result").getChild("rowset").getChildren();
		for(Element characterElement: resultSet){
			Character character = new CharacterImpl(characterElement);
			characters.add(character);
		}
	}
	
	public List<Character> getCharacters()
	{
		return characters;
	}
	
	public void writeObject(ObjectOutputStream os) {
		try {
			os.defaultWriteObject();
			os.writeObject(characters);
		} catch (Exception e) { e.printStackTrace();  }
	}

	@SuppressWarnings("unchecked")
	public void readObject (ObjectInputStream is) {
		try {
			is.defaultReadObject();
			characters = ((List<Character>)is.readObject());
		} catch (Exception e) {e.printStackTrace();}
	}
}
